﻿/*
 * Created by SharpDevelop.
 * User: fred
 * Date: 10/09/2011
 * Time: 09:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

using MetaVideoEditorEvolved.AddIn.HostViews;
using MetaVideoEditorEvolved.Engine.Entity;
using MetaVideoEditorEvolved.Helper;

namespace MetaVideoEditorEvolved.Engine
{
	/// <summary>
	/// Description of AddInManager.
	/// </summary>
	[Serializable]
	public class AddInManager
	{
		protected List<AddInInformation> addInsInformation= new List<AddInInformation>();
		protected string addInPath;
		
		public AddInManager()
		{
			#if DEBUG
			addInPath = Environment.CurrentDirectory;
			#else
			addInPath = ApplicationPath.AddInConfiguration;
			#endif
			
			AddInStore.Rebuild(addInPath);
		}
		
		public List<AddInInformation> AddIns { get
			{
				return addInsInformation;
			}
			
			set
			{
				addInsInformation = value;
			}
		}
		
		public virtual void SaveAllConfiguration()
		{
			foreach (var element in addInsInformation) {
				SaveConfigurationFileAddIn(element);
			}
		}
		
		protected void InsertAddIns(Collection<AddInToken> tokens, AddInType type)
		{
			if(tokens.Any())
			{
				List<AddInToken> addinTemporary = new List<AddInToken>();
				
				for (int i = 0; i < tokens.Count; i++) {
					AddInToken token = tokens[i];

					Tracer.TracerInstance.TraceInformation("Try to insert the AddIn : " + token.Name);
					
					var temporaryAddIn = LoadConfigurationFileAddIn(token);
					
					if(temporaryAddIn == null)
					{
						addinTemporary.Add(token);
					}
					else
					{
						temporaryAddIn.Token = token;
						temporaryAddIn.Publisher = token.Publisher;
						temporaryAddIn.Version = new Version(token.Version);
						temporaryAddIn.Description = token.Description;
						temporaryAddIn.AssemblyId = Guid.NewGuid();
						
						Tracer.TracerInstance.TraceInformation("The configuration file is found for the AddIn : " + token.Name);
						
						addInsInformation.Add(temporaryAddIn);
					}
				}
				
				foreach (var token in addinTemporary) {
					var temporaryAddIn = new AddInInformation()
					{
						Index = addInsInformation.Count - 1,
						Description = token.Description,
						Name = token.Name,
						Publisher = token.Publisher,
						Version = new Version(token.Version),
						Type = type,
						Token = token,
						IsEnable = true,
						RetrieveProperty =new List<AddInProperty>(),
						AssemblyId = Guid.NewGuid()
					};
					
					foreach (var addInProperty in Enum.GetNames(typeof(AddInProperty))) {
						temporaryAddIn.RetrieveProperty.Add((AddInProperty) Enum.Parse(typeof(AddInProperty), addInProperty));
					}
					
					SaveConfigurationFileAddIn(temporaryAddIn);
					
					Tracer.TracerInstance.TraceInformation("Create the configuration file for the AddIn : " + token.Name);
					
					addInsInformation.Add(temporaryAddIn);
				}
			}
			
			AddIns = (from addin in AddIns
					 orderby addin.Index
					 select addin).ToList();
		}
		
		public void LoadAllConfigurationFileAddIn()
		{
			foreach (var addin in AddIns) {
				LoadConfigurationFileAddIn(addin.Token);
			}
		}
		
		private AddInInformation LoadConfigurationFileAddIn(AddInToken token)
		{
			AddInInformation addInInformation  = null;
			XmlSerializer xs = new XmlSerializer(typeof(AddInInformation));
			var addInPath = Path.Combine(ApplicationPath.AddInConfiguration, token.Name.Replace(" ", string.Empty) + ".xml" );
			
			if(File.Exists(addInPath))
			{
				using (StreamReader sr = new StreamReader(addInPath))
				{
					addInInformation = xs.Deserialize(sr) as AddInInformation;
				}
			}
			
			return addInInformation;
		}
		
		private void SaveConfigurationFileAddIn(AddInInformation addInInformation)
		{
			XmlSerializer xs = new XmlSerializer(typeof(AddInInformation));
			
			using(StreamWriter sw = new StreamWriter(Path.Combine(ApplicationPath.AddInConfiguration, addInInformation.Name.Replace(" ", string.Empty) + ".xml" )))
			{
				xs.Serialize(sw, addInInformation);
			}
		}
	}
}
